package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import sun.security.krb5.internal.ASRep;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.common.StringUtil;
import com.windfone.model.Askreponse;
import com.windfone.model.Member;
import com.windfone.service.AskReponseService;
import com.windfone.service.MemberService;

public class AskReponseAction extends ActionSupport {
	private AskReponseService askReponseService;
	private MemberService memberService;
	private int msgtype;
	private int productId;
	private int askId;
	private String user;
	private int reponseId;
	private String reponseName;
	private String askcontent;
	private String reponsecontent;
	private String password;
	private String loginName;
	private Askreponse askreponse;
	private List<Askreponse>  askReponses;
	public Askreponse getAskreponse() {
		return askreponse;
	}
	public void setAskreponse(Askreponse askreponse) {
		this.askreponse = askreponse;
	}

	
	public String getPassword() {
		return password;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public int getMsgtype() {
		return msgtype;
	}
	public int getProductId() {
		return productId;
	}
	public int getAskId() {
		return askId;
	}
	public String getUser() {
		return user;
	}
	public int getReponseId() {
		return reponseId;
	}
	public String getReponseName() {
		return reponseName;
	}
	public String getAskcontent() {
		return askcontent;
	}
	public String getReponsecontent() {
		return reponsecontent;
	}
 
	public void setAskReponseService(AskReponseService askReponseService) {
		this.askReponseService = askReponseService;
	}
	public void setMsgtype(int msgtype) {
		this.msgtype = msgtype;
	}
	public void setProductId(int productId) {
		this.productId = productId;
	}
	public void setAskId(int askId) {
		this.askId = askId;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public void setReponseId(int reponseId) {
		this.reponseId = reponseId;
	}
	public void setReponseName(String reponseName) {
		this.reponseName = reponseName;
	}
	public void setAskcontent(String askcontent) {
		this.askcontent = askcontent;
	}
	public void setReponsecontent(String reponsecontent) {
		this.reponsecontent = reponsecontent;
	}
 
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String askReponseBeforeList() throws Exception {
		return SUCCESS;
	}
	
	public AskReponseService getAskReponseService() {
		return askReponseService;
	}
	public MemberService getMemberService() {
		return memberService;
	}
	public List<Askreponse> getAskReponses() {
		return askReponses;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public String getBeginDate() {
		return beginDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public String getDealFlagStr() {
		return dealFlagStr;
	}
	public void setAskReponses(List<Askreponse> askReponses) {
		this.askReponses = askReponses;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public void setDealFlagStr(String dealFlagStr) {
		this.dealFlagStr = dealFlagStr;
	}

	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private String beginDate;
	private String endDate;
	private String dealFlagStr = "";
	
	private int askreponseRecordId;
	public int getAskreponseRecordId() {
		return askreponseRecordId;
	}
	public void setAskreponseRecordId(int askreponseRecordId) {
		this.askreponseRecordId = askreponseRecordId;
	}
	public String reponseAsk() throws Exception {
		askreponse = new Askreponse();
		askreponse.setId( askreponseRecordId );
		askreponse.setReponsecontent(reponsecontent);
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    if ( session.getAttribute( "userName") !=null){
	    	askreponse.setReponseName( ((String) session.getAttribute( "userName")) );
	    }
		askReponseService.reponseAsk(askreponse);
		return SUCCESS;
	}
	public String beforReponse() throws Exception {
		askreponse = askReponseService.getAskReponseById( askreponseRecordId );
		return SUCCESS;
	}
	public String deleteAsk() throws Exception {
		askreponse = new Askreponse();
		askreponse.setId( askreponseRecordId );
		askReponseService.deleAsk(askreponse );
		return SUCCESS;
	}
	public String askReponseList() throws Exception {
		
		String whereCondition = "  ";
 
		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND a.asktime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND a.asktime<='"+ endDate  +"'  ";
		}
		if ( dealFlagStr!=null && !dealFlagStr.equals("") && !dealFlagStr.equals("-1")){
			whereCondition = whereCondition + "  AND a.dealFlag="+ dealFlagStr  +"";
		}
		if ( msgtype!=-1 ){
			
			whereCondition = whereCondition + " AND msgtype="+msgtype ;
		}
		 
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		
		int totalRecords = askReponseService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		askReponses = askReponseService.geAskAccordByCondition(beginRecord,whereCondition);
		return SUCCESS;
	}
	
	public String customerAsk() throws Exception {
		boolean isLoginUser = false;
		Member memberBean = null;
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object userBean = session.getAttribute("userBean");
		if( userBean==null && password !=null && !password.equals("") ){
			String tmp = StringUtil.encode(password);
			if( isInteger (loginName) ){
				memberBean= memberService.loginByMobile(loginName, tmp);
				if ( memberBean != null ){
				    session.setAttribute("userBean", memberBean);
				    isLoginUser = true;
				}
			} else {
				memberBean = memberService.loginByLoginName(loginName, tmp);
				if ( memberBean != null ){
				    session.setAttribute("userBean", memberBean);
				    isLoginUser = true;
				}
			}
		} else {
			memberBean = (Member)userBean;
		}
		Askreponse askreponse = new Askreponse();
		askreponse.setAskcontent(askcontent);
		if ( isLoginUser ){
			askreponse.setUser(memberBean.getLoginName());
			askreponse.setAskId(memberBean.getId());
		}else{
			askreponse.setUser("顾客");
			askreponse.setAskId(askId);
		}
		askreponse.setMsgtype(msgtype);
		askreponse.setProductId(productId);
		askReponseService.addAsk(askreponse );
		return SUCCESS;
	}
	private  boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}
